var express = require('express');
var router = express.Router();
const { GreetingModel } = require('../model/mongomodule')


/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});



// 获取数据接口
router.get('/gretting', async (req, res) => {
  let data = await GreetingModel.find({},{__v: 0}).exec()
  res.send({
    msg: "获取成功",
    code: 200,
    data,
  })
})

// 添加数据接口
router.post('/gretting', async (req, res) => {
    let data = req.body
    if (Object.keys(data).length === 0) return res.send({
        msg: '添加失败，添加内容不能为空',
        code: 401,
    })
    await GreetingModel.create(data)
    res.send({
      msg: '添加成功',
      code: 201
    })
})



module.exports = router;
